package gui.framework;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JFrame;

public class WindowTools {
	/**
	 * 将一个容器的位置设置为居中
	 * 
	 * @param frame
	 *            将要调整的容器
	 */
	public static void locateFrameCenter(JFrame frame) {
		int frameWidth = frame.getWidth();
		int frameHeight = frame.getHeight();
		/*
		 * Toolkit是所有 Abstract Window Toolkit 实际实现的抽象超类。 getDefaultToolkit()
		 * 获取默认工具包。
		 */
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		/*
		 * Dimension 类封装单个对象中组件的宽度和高度（精确到整数）。 getScreenSize() 获取当前显示器尺寸
		 */
		Dimension screen = toolkit.getScreenSize();
		int screenWidth = screen.width;
		int screenHeight = screen.height;
		/*
		 * 调整当前容器位置居中
		 */
		frame.setLocation((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2);
	}

	/**
	 * 设置对话框居中显示
	 * 
	 * @param dialog
	 *            将要调整的对话框
	 */
	public static void locateDialogCenter(JDialog dialog) {
		int frameWidth = dialog.getWidth();
		int frameHeight = dialog.getHeight();
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screen = toolkit.getScreenSize();
		int screenWidth = screen.width;
		int screenHeight = screen.height;
		dialog.setLocation((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2);
	}
}
